#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
步骤条组件示例程序
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
                            QHBoxLayout, QPushButton, QLabel, QComboBox,
                            QCheckBox, QSpinBox, QGroupBox, QScrollArea,
                            QColorDialog, QLineEdit)

from component import Steps, Theme, Card, VBox, HBox


class MainWindow(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("步骤条组件示例")
        self.resize(800, 600)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_content = QWidget()
        scroll_layout = QVBoxLayout(scroll_content)
        scroll_layout.setAlignment(Qt.AlignTop)
        scroll_area.setWidget(scroll_content)
        main_layout.addWidget(scroll_area)
        
        # 添加基本步骤条示例
        self._add_basic_steps_example(scroll_layout)
        
        # 添加垂直步骤条示例
        self._add_vertical_steps_example(scroll_layout)
        
        # 添加带描述的步骤条示例
        self._add_description_steps_example(scroll_layout)
        
        # 添加自定义步骤条示例
        self._add_custom_steps_example(scroll_layout)
        
        # 添加交互式步骤条示例
        self._add_interactive_steps_example(scroll_layout)
    
    def _add_basic_steps_example(self, parent_layout):
        """添加基本步骤条示例"""
        # 创建卡片
        card = Card("基本步骤条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加步骤条
        steps_data = [
            {"title": "步骤 1", "description": ""},
            {"title": "步骤 2", "description": ""},
            {"title": "步骤 3", "description": ""},
            {"title": "步骤 4", "description": ""}
        ]
        
        # 不同进度的步骤条
        steps_1 = Steps(steps=steps_data, current=0)
        steps_2 = Steps(steps=steps_data, current=1)
        steps_3 = Steps(steps=steps_data, current=2)
        steps_4 = Steps(steps=steps_data, current=3)
        
        # 添加到布局
        vbox.add_widget(QLabel("步骤 1 激活"))
        vbox.add_widget(steps_1)
        vbox.add_widget(QLabel("步骤 2 激活"))
        vbox.add_widget(steps_2)
        vbox.add_widget(QLabel("步骤 3 激活"))
        vbox.add_widget(steps_3)
        vbox.add_widget(QLabel("步骤 4 激活"))
        vbox.add_widget(steps_4)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_vertical_steps_example(self, parent_layout):
        """添加垂直步骤条示例"""
        # 创建卡片
        card = Card("垂直步骤条")
        
        # 创建水平布局
        hbox = HBox(margin=10, spacing=20)
        
        # 添加步骤条
        steps_data = [
            {"title": "步骤 1", "description": ""},
            {"title": "步骤 2", "description": ""},
            {"title": "步骤 3", "description": ""},
            {"title": "步骤 4", "description": ""}
        ]
        
        # 不同进度的垂直步骤条
        steps_1 = Steps(steps=steps_data, current=1, direction="vertical")
        steps_2 = Steps(steps=steps_data, current=2, direction="vertical")
        
        # 添加到布局
        hbox.add_widget(steps_1)
        hbox.add_widget(steps_2)
        
        # 添加到卡片
        card.add_widget(hbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_description_steps_example(self, parent_layout):
        """添加带描述的步骤条示例"""
        # 创建卡片
        card = Card("带描述的步骤条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加步骤条
        steps_data = [
            {"title": "注册", "description": "创建账号"},
            {"title": "验证", "description": "验证邮箱"},
            {"title": "完善信息", "description": "填写个人资料"},
            {"title": "完成", "description": "开始使用"}
        ]
        
        # 水平带描述步骤条
        steps_h = Steps(steps=steps_data, current=2, simple=False)
        
        # 垂直带描述步骤条
        steps_v = Steps(steps=steps_data, current=2, direction="vertical", simple=False)
        
        # 添加到布局
        vbox.add_widget(QLabel("水平带描述步骤条"))
        vbox.add_widget(steps_h)
        vbox.add_widget(QLabel("垂直带描述步骤条"))
        vbox.add_widget(steps_v)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_custom_steps_example(self, parent_layout):
        """添加自定义步骤条示例"""
        # 创建卡片
        card = Card("自定义步骤条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加步骤条
        steps_data = [
            {"title": "选择产品", "description": ""},
            {"title": "填写订单", "description": ""},
            {"title": "付款", "description": ""},
            {"title": "完成", "description": ""}
        ]
        
        # 自定义颜色的步骤条
        steps_custom_color = Steps(
            steps=steps_data,
            current=2,
            active_color="#FF5722",  # 橙色
            finished_color="#4CAF50",  # 绿色
            inactive_color="#9E9E9E"   # 灰色
        )
        
        # 自定义间距和点大小的步骤条
        steps_custom_size = Steps(
            steps=steps_data,
            current=2,
            space=150,  # 更大的间距
            dot_size=32,  # 更大的点
            line_width=3  # 更粗的线
        )
        
        # 简洁模式步骤条
        steps_simple = Steps(
            steps=steps_data,
            current=2,
            simple=True
        )
        
        # 添加到布局
        vbox.add_widget(QLabel("自定义颜色"))
        vbox.add_widget(steps_custom_color)
        vbox.add_widget(QLabel("自定义大小和间距"))
        vbox.add_widget(steps_custom_size)
        vbox.add_widget(QLabel("简洁模式"))
        vbox.add_widget(steps_simple)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_interactive_steps_example(self, parent_layout):
        """添加交互式步骤条示例"""
        # 创建卡片
        card = Card("交互式步骤条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加步骤条
        steps_data = [
            {"title": "第一步", "description": "开始"},
            {"title": "第二步", "description": "处理中"},
            {"title": "第三步", "description": "检查"},
            {"title": "第四步", "description": "完成"}
        ]
        
        # 创建交互式步骤条
        self.interactive_steps = Steps(steps=steps_data, current=0)
        
        # 创建控制按钮
        control_layout = QHBoxLayout()
        
        self.prev_button = QPushButton("上一步")
        self.prev_button.clicked.connect(self._prev_step)
        self.prev_button.setEnabled(False)  # 初始禁用
        
        self.next_button = QPushButton("下一步")
        self.next_button.clicked.connect(self._next_step)
        
        control_layout.addWidget(self.prev_button)
        control_layout.addWidget(self.next_button)
        
        # 创建自定义控制面板
        control_group = QGroupBox("控制面板")
        control_panel_layout = QVBoxLayout(control_group)
        
        # 方向选择
        direction_layout = QHBoxLayout()
        direction_layout.addWidget(QLabel("方向:"))
        self.direction_combo = QComboBox()
        self.direction_combo.addItems(["horizontal", "vertical"])
        self.direction_combo.currentTextChanged.connect(self._change_direction)
        direction_layout.addWidget(self.direction_combo)
        control_panel_layout.addLayout(direction_layout)
        
        # 简洁模式选择
        simple_layout = QHBoxLayout()
        self.simple_check = QCheckBox("简洁模式")
        self.simple_check.stateChanged.connect(self._toggle_simple)
        simple_layout.addWidget(self.simple_check)
        control_panel_layout.addLayout(simple_layout)
        
        # 间距调整
        space_layout = QHBoxLayout()
        space_layout.addWidget(QLabel("间距:"))
        self.space_spin = QSpinBox()
        self.space_spin.setRange(50, 200)
        self.space_spin.setValue(100)
        self.space_spin.valueChanged.connect(self._change_space)
        space_layout.addWidget(self.space_spin)
        control_panel_layout.addLayout(space_layout)
        
        # 点大小调整
        dot_size_layout = QHBoxLayout()
        dot_size_layout.addWidget(QLabel("点大小:"))
        self.dot_size_spin = QSpinBox()
        self.dot_size_spin.setRange(16, 40)
        self.dot_size_spin.setValue(24)
        self.dot_size_spin.valueChanged.connect(self._change_dot_size)
        dot_size_layout.addWidget(self.dot_size_spin)
        control_panel_layout.addLayout(dot_size_layout)
        
        # 线宽调整
        line_width_layout = QHBoxLayout()
        line_width_layout.addWidget(QLabel("线宽:"))
        self.line_width_spin = QSpinBox()
        self.line_width_spin.setRange(1, 5)
        self.line_width_spin.setValue(2)
        self.line_width_spin.valueChanged.connect(self._change_line_width)
        line_width_layout.addWidget(self.line_width_spin)
        control_panel_layout.addLayout(line_width_layout)
        
        # 添加到布局
        vbox.add_widget(QLabel("交互式步骤条"))
        vbox.add_widget(self.interactive_steps)
        
        buttons_widget = QWidget()
        buttons_widget.setLayout(control_layout)
        vbox.add_widget(buttons_widget)
        
        vbox.add_widget(control_group)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _next_step(self):
        """前进到下一步"""
        if self.interactive_steps.next_step():
            self.prev_button.setEnabled(True)
            if self.interactive_steps.get_current() == len(self.interactive_steps.get_steps()) - 1:
                self.next_button.setEnabled(False)
    
    def _prev_step(self):
        """返回上一步"""
        if self.interactive_steps.prev_step():
            self.next_button.setEnabled(True)
            if self.interactive_steps.get_current() == 0:
                self.prev_button.setEnabled(False)
    
    def _change_direction(self, direction):
        """改变步骤条方向"""
        self.interactive_steps.set_direction(direction)
    
    def _toggle_simple(self, state):
        """切换简洁模式"""
        self.interactive_steps.set_simple(state == Qt.Checked)
    
    def _change_space(self, value):
        """改变步骤间距"""
        self.interactive_steps.set_space(value)
    
    def _change_dot_size(self, value):
        """改变点大小"""
        self.interactive_steps.set_dot_size(value)
    
    def _change_line_width(self, value):
        """改变线宽"""
        self.interactive_steps.set_line_width(value)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 应用主题
    Theme.apply_theme(app)
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_()) 